#!/usr/bin/env bash

create_archive() {
	local i DEB_SOURCE
	DEB_SOURCE="$(realpath "$RSDK_OPTION_DEB_DIR")"

	for i in "$@"; do
		aptly publish drop -force-drop "$i" "${RSDK_REPO_ORIGIN:-.}" || true
		aptly repo drop -force "$i" || true
	done
	aptly db cleanup

	for i in "$@"; do
		aptly repo create -distribution="$i" "$i"
		aptly repo add "$i" "$DEB_SOURCE/$i"
		local aptly_args=()
		if [[ -n $RSDK_REPO_ORIGIN ]]; then
			aptly_args+=("-origin=$RSDK_REPO_ORIGIN")
		fi
		if [[ -n $RSDK_REPO_LABEL ]]; then
			aptly_args+=("-label=$RSDK_REPO_LABEL")
		fi
		aptly publish repo "${aptly_args[@]}" -skip-signing -architectures=all,arm64,armhf,riscv64 "$i" "$RSDK_REPO_ORIGIN"
	done

	if [[ $RSDK_OPTION_SKIP_SIGNING == "false" ]]; then
		local GPG_PARAMETERS=(
			"--yes"
			"--armor"
		)

		local keyrings=() key_missing="false"
		mapfile -t keyrings < <("$SCRIPT_DIR/../../bin/rsdk" config infra.repository.keyrings) && array_remove "keyrings" ""
		for i in "${keyrings[@]}"; do
			if ! gpg -K "$i" &>/dev/null; then
				echo "Private key for '$i' is not available!" >&2
				key_missing="true"
			fi
			GPG_PARAMETERS+=("-u" "$i")
		done

		if [[ $key_missing == "true" ]]; then
			echo "Signing failed due to missing private key." >&2
			return 1
		fi

		for i in "$@"; do
			echo "Signing '$i'..."
			local DISTRO_PATH="$HOME/.aptly/public/$RSDK_REPO_ORIGIN/dists/$i"
			gpg "${GPG_PARAMETERS[@]}" --clear-sign -o "$DISTRO_PATH/InRelease" "$DISTRO_PATH/Release"
			gpg "${GPG_PARAMETERS[@]}" --detach-sign -o "$DISTRO_PATH/Release.gpg" "$DISTRO_PATH/Release"
		done
	fi
}

main() {
	local SCRIPT_DIR
	SCRIPT_DIR="$(dirname "$(realpath "$0")")"
	# shellcheck source=src/lib/rsdk/utils.sh
	source "$SCRIPT_DIR/../../lib/rsdk/utils.sh"
	# shellcheck source=src/lib/rsdk/stdlib.sh
	source "$SCRIPT_DIR/../../lib/rsdk/stdlib.sh"

	local TEMP
	if ! TEMP="$(getopt -o "hds" -l "help,dry-run,skip-signing,deb-dir:,origin:,label:" -n "$0" -- "$@")"; then
		return
	fi
	eval set -- "$TEMP"

	export RSDK_OPTION_DRY_RUN="${RSDK_OPTION_DRY_RUN:-false}"
	export RSDK_OPTION_SKIP_SIGNING="${RSDK_OPTION_SKIP_SIGNING:-false}"
	export RSDK_OPTION_DEB_DIR="${RSDK_OPTION_DEB_DIR:-${TMPDIR:-/tmp}/rsdk-deb}"
	export RSDK_REPO_ORIGIN="${RSDK_REPO_ORIGIN:-$("$SCRIPT_DIR/../../bin/rsdk" config infra.repository.origin)}"
	export RSDK_REPO_LABEL="${RSDK_REPO_LABEL:-$("$SCRIPT_DIR/../../bin/rsdk" config infra.repository.label)}"

	while true; do
		TEMP="$1"
		shift
		case "$TEMP" in
		-d | --dry-run)
			RSDK_OPTION_DRY_RUN="true"
			;;
		-h | --help)
			TEMP="$(basename "$0")"
			rsdk help "${TEMP#rsdk-}"
			return
			;;
		-s | --skip-signing)
			RSDK_OPTION_SKIP_SIGNING="true"
			;;
		--deb-dir)
			RSDK_OPTION_DEB_DIR="$1"
			shift
			;;
		--origin)
			RSDK_REPO_ORIGIN="$1"
			shift
			;;
		--label)
			RSDK_REPO_LABEL="$1"
			shift
			;;
		--)
			break
			;;
		*)
			error "$EXIT_UNKNOWN_OPTION" "$TEMP"
			;;
		esac
	done

	if (($# == 0)); then
		"$0" --help
		return
	elif [[ ! -e $RSDK_OPTION_DEB_DIR ]]; then
		echo "deb-dir is set to '$RSDK_OPTION_DEB_DIR' but it does not exist!" >&2
		return 1
	fi

	create_archive "$@"
}

main "$@"
